/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GerenciadorSalas.java
 *
 * Created on 28/05/2011, 11:46:46
 */
package gui;

import fachadas.*;
import entidades.*;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 * 
 * @author Thiago
 */
public class GerenciadorSalas extends javax.swing.JFrame {

	/** Creates new form GerenciadorSalas */
	public GerenciadorSalas() {
		modelSalas = new DefaultListModel();
		gerSalas = new UserInterfaceFacade();
		salas = new ArrayList<Sala>();
		salvar();
		initComponents();
		setLocationRelativeTo(null);
		atualizaSalasDoGerenciador();
		atualiza();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup_Aberta = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_ID = new javax.swing.JTextField();
        jTextField_Capac = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField_Apelido = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton_Cadastrar = new javax.swing.JButton();
        jComboBox_Estado = new javax.swing.JComboBox();
        jComboBox_Finalidade = new javax.swing.JComboBox();
        jComboBox_Tipo = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList_Salas = new javax.swing.JList();
        jLabel9 = new javax.swing.JLabel();
        jButton_Remover = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField_RemoveID = new javax.swing.JTextField();
        jButton_Update = new javax.swing.JButton();
        jButton_RemoveID = new javax.swing.JButton();
        jButton_Voltar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Gerenciador de Salas");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Para cadastrar uma nova sala no sistema, basta preencher os campos abaixo.");

        jLabel2.setText("* ID:");

        jLabel3.setText("* Capacidade:");

        jLabel4.setText("* Finalidade:");

        jLabel5.setText("* Tipo:");

        jTextField_Apelido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_ApelidoActionPerformed(evt);
            }
        });

        jLabel6.setText("Apelido:");

        jLabel7.setText("Estado:");

        jLabel8.setText("* Campos Obrigatórios");

        jButton_Cadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/okIcon.png"))); // NOI18N
        jButton_Cadastrar.setText("Cadastrar Sala");
        jButton_Cadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CadastrarActionPerformed(evt);
            }
        });

        jComboBox_Estado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "< Nenhum >", "Aberta", "Fechada" }));

        jComboBox_Finalidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sala De Aula", "Laboratorio", "Escritorio", "Sala de Conferencia" }));
        jComboBox_Finalidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_FinalidadeActionPerformed(evt);
            }
        });

        jComboBox_Tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Inteligente", "Videoconferencia" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(18, 18, 18))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jComboBox_Tipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jTextField_Capac, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jComboBox_Finalidade, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jTextField_ID, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                                        .addComponent(jTextField_Apelido, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                                    .addComponent(jComboBox_Estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE)
                        .addComponent(jButton_Cadastrar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jTextField_ID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField_Capac, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jComboBox_Finalidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jComboBox_Tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField_Apelido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jComboBox_Estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
                        .addComponent(jLabel8))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton_Cadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))))
        );

        jTabbedPane1.addTab("Adicionar Sala", jPanel1);

        jList_Salas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "< Não existe nenhuma sala adicionada >" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList_Salas);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Para remover uma sala do sistema, basta selecioná-la e clicar no botão abaixo.");

        jButton_Remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/exitIcon.png"))); // NOI18N
        jButton_Remover.setText("Remover Sala");
        jButton_Remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_RemoverActionPerformed(evt);
            }
        });

        jLabel10.setText("Remova também pelo ID:");

        jLabel11.setText("ID:");

        jButton_Update.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/updateIcon.png"))); // NOI18N
        jButton_Update.setText("Atualizar");
        jButton_Update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_UpdateActionPerformed(evt);
            }
        });

        jButton_RemoveID.setText("Remover");
        jButton_RemoveID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_RemoveIDActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel11)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTextField_RemoveID))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton_Update)
                                    .addComponent(jButton_Remover))
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton_RemoveID)))
                    .addComponent(jLabel9))
                .addGap(27, 27, 27))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton_Remover, jButton_Update});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jTextField_RemoveID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton_RemoveID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
                        .addComponent(jButton_Update)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton_Remover)
                        .addGap(19, 19, 19))))
        );

        jTabbedPane1.addTab("Remover Sala", jPanel2);

        jButton_Voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/backIcon.png"))); // NOI18N
        jButton_Voltar.setText("Voltar");
        jButton_Voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_VoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(524, Short.MAX_VALUE)
                .addComponent(jButton_Voltar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jButton_Voltar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jTextField_ApelidoActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jTextField_ApelidoActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jTextField_ApelidoActionPerformed

	private void jButton_VoltarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_VoltarActionPerformed
		salvar();
		new TelaPrincipal().setVisible(true);
		dispose();
	}// GEN-LAST:event_jButton_VoltarActionPerformed

	private void jButton_CadastrarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_CadastrarActionPerformed
		String id = jTextField_ID.getText();
		String capacidadeStr = jTextField_Capac.getText();
		String finalidade = jComboBox_Finalidade.getSelectedItem().toString();
		String apelido = jTextField_Apelido.getText();
		String tipo = jComboBox_Tipo.getSelectedItem().toString();
		int capacidade = 0;
		String msgErro = "";

		if (capacidadeValida(capacidadeStr)) {
			capacidade = Integer.parseInt(capacidadeStr.trim());
		}

		if (textoEmBranco(id)) {
			msgErro += "ID Invalido\n";
		}

		if (capacidade == 0) {
			msgErro += "Capacidade Invalida\n";
		}

		if (!msgErro.isEmpty()) {
			JOptionPane.showMessageDialog(null,
					"Verifique se esses campos foram\n"
							+ "preenchidos corretamente:\n\n" + msgErro,
					"Erro Ao Cadastrar", JOptionPane.ERROR_MESSAGE);

		} else {
			try {

				if (textoEmBranco(apelido) && nenhumaSelecao()) {
					gerSalas.adicionarSala(id, capacidade, finalidade, tipo);
					showSucesso();
					
				} else if (nenhumaSelecao()) {
					gerSalas.adicionarSala(id, capacidade, finalidade, tipo,
							apelido);
					showSucesso();
				} else if (!textoEmBranco(apelido)) {
					if (salaAberta()) {
						gerSalas.adicionarSala(id, capacidade, finalidade,
								tipo, apelido, true);
						showSucesso();
					}
					if (salaFechada()) {
						gerSalas.adicionarSala(id, capacidade, finalidade,
								tipo, apelido, false);
						showSucesso();
					}
				} else {
					JOptionPane
							.showMessageDialog(
									null,
									"Verifique se esses campos foram\n"
											+ "preenchidos corretamente:\n\n"
											+ "Para adicionar um estado a sala\n eh necessario atribuir um apelido\n",
									"Erro Ao Cadastrar",
									JOptionPane.ERROR_MESSAGE);
				}

			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null,
						"Verifique se esses campos foram\n"
								+ "preenchidos corretamente:\n\n"
								+ ex.getMessage(), "Erro Ao Cadastrar",
						JOptionPane.ERROR_MESSAGE);
			}
		}

		atualizaSalasDoGerenciador();
		atualiza();
		salvar();

	}// GEN-LAST:event_jButton_CadastrarActionPerformed

	private void jButton_UpdateActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_UpdateActionPerformed
		atualiza();
	}// GEN-LAST:event_jButton_UpdateActionPerformed

	private void jButton_RemoverActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_RemoverActionPerformed
		Sala sala = null;

		// Caso a sala nao esteja selecionado
		try {
			sala = salas.get(jList_Salas.getSelectedIndex());
			gerSalas.removerSala(sala.getId());
			salvar();
		} catch (Exception e) {
			if (jList_Salas.isSelectionEmpty()) {
				JOptionPane.showMessageDialog(null,
						"Erro ao Remover uma Sala:\n\n"
								+ "Selecione uma Sala para remocao",
						"Erro na Remocao", JOptionPane.ERROR_MESSAGE);
			}
			return;
		}
		atualizaSalasDoGerenciador();
		atualiza();
	}// GEN-LAST:event_jButton_RemoverActionPerformed

	private void jButton_RemoveIDActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_RemoveIDActionPerformed
		String id = jTextField_RemoveID.getText();

		if (textoEmBranco(id)) {
			JOptionPane.showMessageDialog(null, "Erro ao Remover uma Sala:\n\n"
					+ "Digite algum ID para remover", "Erro na Remocao",
					JOptionPane.ERROR_MESSAGE);
		} else {
			try {
				gerSalas.removerSala(id);
				salvar();
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null,
						"Erro ao Remover uma Sala:\n\n" + "Sala Nao Existente",
						"Erro na Remocao", JOptionPane.ERROR_MESSAGE);
			}
		}
		atualizaSalasDoGerenciador();
		atualiza();

	}// GEN-LAST:event_jButton_RemoveIDActionPerformed

	private void jComboBox_FinalidadeActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jComboBox_FinalidadeActionPerformed
		String finalidade = jComboBox_Finalidade.getSelectedItem().toString();

		if (finalidade.equals("Sala De Aula")) {
			jComboBox_Tipo
					.setModel(new javax.swing.DefaultComboBoxModel(
							new String[] { "Normal", "Inteligente",
									"Videoconferencia" }));
		} else if (finalidade.equals("Laboratorio")) {
			jComboBox_Tipo.setModel(new javax.swing.DefaultComboBoxModel(
					new String[] { "Biologia", "Quimica", "Fisica",
							"Computacao" }));
		} else if (finalidade.equals("Escritorio")) {
			jComboBox_Tipo.setModel(new javax.swing.DefaultComboBoxModel(
					new String[] { "< Nenhum >" }));
		} else if (finalidade.equals("Sala de Conferencia")) {
			jComboBox_Tipo.setModel(new javax.swing.DefaultComboBoxModel(
					new String[] { "Normal", "Videoconferencia" }));
		}
	}// GEN-LAST:event_jComboBox_FinalidadeActionPerformed

	private void showSucesso() {
		JOptionPane.showMessageDialog(null,
				"Sala foi adicionada com sucesso!\n\n", "Sala cadastrada",
				JOptionPane.INFORMATION_MESSAGE);
	}

	private void atualiza() {
		modelSalas = new DefaultListModel();
		int numSalas = salas.size();

		if (numSalas > 0) {
			for (int i = 0; i < numSalas; i++) {
				modelSalas.add(i, salaFormatada(salas.get(i)));
			}
		}

		jList_Salas.setModel(modelSalas);
		jList_Salas.updateUI();

	}

	// Metodo com objetivo de converter Alocaveis para Sala
	private void atualizaSalasDoGerenciador() {
		int numSalas = gerSalas.getListaDeSalas().size();

		salas = new ArrayList<Sala>();

		Sala salaAtual = null;
		for (int i = 0; i < numSalas; i++) {
			salaAtual = (Sala) gerSalas.getListaDeSalas().get(i);
			salas.add(salaAtual);
		}
	}

	private String salaFormatada(Sala sala) {
		String saida = "";

		saida = sala.getId() + " - " + sala.getFinalidade() + " - "
				+ sala.getTipo() + " - " + sala.getApelido();

		return saida;
	}

	private boolean textoEmBranco(String texto) {
		return (texto.replace(" ", "").equals(""));
	}

	private boolean capacidadeValida(String capacidade) {

		if (textoEmBranco(capacidade)) {
			return false;
		}

		for (int i = 0; i < capacidade.length(); i++) {
			if (!(Character.isDigit(capacidade.charAt(i)))) {
				return false;
			}
		}
		return true;

	}

	private void salvar() {
		gerSalas.menuSair();
	}

	private boolean nenhumaSelecao() {
		return (jComboBox_Estado.getSelectedItem().toString()
				.equals("< Nenhum >"));
	}

	private boolean salaAberta() {
		return (jComboBox_Estado.getSelectedItem().toString().equals("Aberta"));
	}

	private boolean salaFechada() {
		return (jComboBox_Estado.getSelectedItem().toString().equals("Fechada"));
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new GerenciadorSalas().setVisible(true);
			}
		});
	}

	private DefaultListModel modelSalas;
	private UserInterfaceFacade gerSalas;
	private ArrayList<Sala> salas;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup_Aberta;
    private javax.swing.JButton jButton_Cadastrar;
    private javax.swing.JButton jButton_RemoveID;
    private javax.swing.JButton jButton_Remover;
    private javax.swing.JButton jButton_Update;
    private javax.swing.JButton jButton_Voltar;
    private javax.swing.JComboBox jComboBox_Estado;
    private javax.swing.JComboBox jComboBox_Finalidade;
    private javax.swing.JComboBox jComboBox_Tipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList_Salas;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField_Apelido;
    private javax.swing.JTextField jTextField_Capac;
    private javax.swing.JTextField jTextField_ID;
    private javax.swing.JTextField jTextField_RemoveID;
    // End of variables declaration//GEN-END:variables
}
